package com.zxl.admin.service;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.zxl.common.constant.DeleteStatusConstant;
import com.zxl.common.exception.BusinessException;
import com.zxl.common.mapper.SysGoodsMapper;
import com.zxl.common.model.bo.goods.GoodsAdd;
import com.zxl.common.model.bo.goods.GoodsCgt;
import com.zxl.common.model.pojo.SysGoods;
import com.zxl.common.model.query.goods.GoodsListQuery;
import com.zxl.common.model.vo.goods.GoodsListVo;
import com.zxl.common.service.SysGoodsService;
import com.zxl.common.utils.GoodsUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.StringJoiner;

/**
 * @author zxw
 * @date 2023/3/31
 */
@Service
public class GoodsService {
    @Resource
    private SysGoodsMapper sysGoodsMapper;
    @Resource
    private SysGoodsService sysGoodsService;

    @Transactional(rollbackFor = Exception.class)
    public void addGoods(GoodsAdd goodsAdd) {
        SysGoods goods = new SysGoods();
        BeanUtil.copyProperties(goodsAdd,goods);
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String detail : goodsAdd.getGoodsDetailImage()) {
            stringJoiner.add(detail);
        }
        goods.setGoodsDetailImage(stringJoiner.toString());
        String goodsCode = GoodsUtils.generatorGoodCode(GoodsUtils.B_PREFIX);
        goods.setGoodsCode(goodsCode);
        sysGoodsMapper.insert(goods);
    }

    public List<GoodsListVo> selectGoodsList(GoodsListQuery goodsListQuery) {
        PageHelper.startPage(goodsListQuery.getPageNumber(), goodsListQuery.getPageSize());
        List<GoodsListVo> goodsList = sysGoodsMapper.selectGoodsList(goodsListQuery);
        return goodsList;
    }

    public void updateGoods(GoodsAdd goodsAdd) {
        SysGoods goods = sysGoodsMapper.selectById(goodsAdd.getId());
        if(goods == null){
            throw new BusinessException("未找到此商品信息");
        }
        BeanUtil.copyProperties(goodsAdd,goods);
        StringJoiner stringJoiner = new StringJoiner(",");
        for (String detail : goodsAdd.getGoodsDetailImage()) {
            stringJoiner.add(detail);
        }
        goods.setGoodsDetailImage(stringJoiner.toString());
        sysGoodsMapper.updateById(goods);
    }

    public void doDeleteGoods(Integer id) {
        if(id == null){
            throw new BusinessException("id不允许为空");
        }
        SysGoods goods = new SysGoods();
        goods.setId(id);
        goods.setDeleteStatus(DeleteStatusConstant.HAS_DELETE.getCode());
        sysGoodsMapper.updateById(goods);
    }

    public void doSetCGoodsStatus(Integer id, Integer status) {
        if(id == null){
            throw new BusinessException("id不允许为空");
        }
        sysGoodsMapper.updateCGoodsStatus(id,status);
    }

    public void updateGoodsCgt(GoodsCgt goodsCgt) {
        SysGoods goods = sysGoodsMapper.selectById(goodsCgt.getId());
        if(goods == null){
            throw new BusinessException("未找到此商品信息");
        }
        SysGoods sysGoods = new SysGoods();
        sysGoods.setId(goodsCgt.getId());
        sysGoods.setGoodsCategoryId(goodsCgt.getCgtId());
        sysGoodsMapper.updateById(sysGoods);
    }
}
